/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SeleccionarFuncion.java
 *
 * Created on 19-jun-2011, 16:55:09
 */
package guiBoleteria;

import java.util.LinkedList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import java.util.List;
import logica.Fabrica;
import logica.IBoleteria;
import util.datatypes.DTFuncion;
import util.datatypes.DTPelicula;
import util.exceptions.NoExistePeliculaException;

/**
 *
 * @author Administrador
 */
public class SeleccionarFuncion extends javax.swing.JDialog {

    private DefaultListModel funciones;
    private DTFuncion funcion;
    private List<DTFuncion> listaFunciones = new LinkedList<DTFuncion> ();
    /** Creates new form SeleccionarFuncion */
    public SeleccionarFuncion(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }
    
    public SeleccionarFuncion(java.awt.Frame parent, boolean modal, DTPelicula p) {
        super(parent, modal);
        initComponents();
        cargarListaFunciones(p);
        lblPelicula.setText(p.getNombre());
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblPelicula = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstFunciones = new javax.swing.JList();
        btnSeleccionar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        lblPelicula.setFont(new java.awt.Font("Arial", 1, 12));
        lblPelicula.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lstFunciones.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        lstFunciones.setFont(new java.awt.Font("Arial", 0, 11));
        lstFunciones.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(lstFunciones);

        btnSeleccionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/pequeñoarrow_right.png"))); // NOI18N
        btnSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/cancel_icon.jpg"))); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPelicula)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(306, Short.MAX_VALUE)
                        .addComponent(btnSeleccionar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar)))
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(lblPelicula)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancelar)
                    .addComponent(btnSeleccionar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarActionPerformed
        if (lstFunciones.getSelectedValue() != null)
        {
            funcion = listaFunciones.get(lstFunciones.getSelectedIndex());
            dispose();
        }
        else
            JOptionPane.showMessageDialog(null, "Debe seleccionar una función de la lista", "Advertencia", JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_btnSeleccionarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        funcion = null;
        dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                SeleccionarFuncion dialog = new SeleccionarFuncion(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    public String getValue() 
    {
        this.setVisible(true);
        if (funcion != null)
            return "" + funcion.getId();
        else return "";
    }
    
    private void cargarListaFunciones(DTPelicula peli)
    {
        try 
        {
            IBoleteria i = Fabrica.getIBoleteria();
            listaFunciones = i.listarFunciones(peli.getId());
            funciones = new DefaultListModel();
            for (DTFuncion dtFuncion: listaFunciones)
            {
                String aux = "Horario: " + i.obtenerHoraString(dtFuncion.getHoraInicio()) + "          Entradas disponibles: " + dtFuncion.getEntradasDisponibles();
                funciones.addElement(aux);
            }

            lstFunciones.setModel(funciones);
        }
        catch (NoExistePeliculaException ex) 
        {
            JOptionPane.showMessageDialog(null, ex.getMessage().toString(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSeleccionar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblPelicula;
    private javax.swing.JList lstFunciones;
    // End of variables declaration//GEN-END:variables
}
